piXoneer Development Library
XDL 3.0 for C#
NXVideoView를 활용하여 동영상 플레이어를 구현해 봅니다. 기본적인 동영상 플레이어 기능을 수행합니다.
프로젝트 이름은 “XDL_VideoView2”로 한다
C#
private void toolStripMenuOpen_Click(object sender, EventArgs e)
{
}
C#
using Pixoneer.NXDL;
using Pixoneer.NXDL.NXVideo;
C#
enum VideoAction { STOP, PLAYING, PAUSED }
struct VideoState
{
public XVideo video; // 파일이나 네트워크로부터 입력되는 스트리밍데이터를 제어하는 기능을 수행할 객체 선언
public XVideoChannel videoChannel; // 동영상 개체에 포함된 채널 객체 선언
public string videoFilePath; // 동영상 파일 경로
public VideoAction action; // 비디오 플레이 상태를 정의하는 객체 선언
}
private XVideoIO m_videoIO = null; // 동영상의 입출력을 담당할 객체 선언
private VideoState VS; // 비디오 상태를 관리하는 객체 선언
C#
enum public Form1()
{
InitializeComponent();
m_videoIO = new XVideoIO(); // VideoIO를 생성
VideoInit();
nxVideoLayerOverlay1.LayerVisible = true;
}
private void VideoInit()
{
// VideoState의 초기화
VS.video = null;
VS.videoChannel = null;
VS.videoFilePath = string.Empty;
VS.action = VideoAction.STOP;
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
// 동영상 채널 정보 초기화
nxVideoView1.ResetVideoChannel();
if (VS.video != null)
{
// 동영상 객체 Close
VS.video.Close();
VS.video = null;
}
if (m_videoIO != null)
{
// 동영상 입출력 객체 Dispose
m_videoIO.Dispose();
}
}
C#
private void toolStripMenuOpen_Click(object sender, EventArgs e)
{
// 새로운 파일 Open을 수행한다.
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "TS file(*.ts)|*.ts||";
openFileDialog.RestoreDirectory = true;
if (openFileDialog.ShowDialog() != DialogResult.OK) return;
string videoPath = openFileDialog.FileName;
// 동영상 파일의 존재 유무 체크
if (System.IO.File.Exists(videoPath) == false)
{
MessageBox.Show(this, "해당 경로에 영상이 없습니다.", "오류");
return;
}
// 파일 경로를 저장한다.
VS.videoFilePath = videoPath;
// 둥영상의 재생상태가 중지가 아닐 경우 동영상 재생 중지
OnStop();
// 동영상 스트리밍 환경을 생성한다.
OnOpen();
// 동영상을 Play한다.
OnPlay();
}
C#
// 동영상 파일 열기
public void OnOpen()
{
try
{
string strError = null;
// 동영상 스트림 정보 가져오기
VS.video = m_videoIO.OpenFile(VS.videoFilePath, "XFFMPDRIVER", out strError);
if (VS.video == null)
{
MessageBox.Show(this, "동영상 재생에 실패하였습니다. 파일을 확인해주십시오.", "파일 열기");
return;
}
// 다중 채널을 가진 동영상 객체의 Channel 인덱스
int nIdxChannel = 0;
// 동영상 뷰에 재생할 동영상 채널을 성정
nxVideoView1.SetVideoChannel(VS.video, nIdxChannel);
// 입력 인덱스에 해당하는 Channel을 가져오기
VS.videoChannel = VS.video.GetChannel(nIdxChannel);
// GetChannel에 실패할 경우 Null 객체가 return되며, 그에 대한 예외처리
if (VS.videoChannel == null)
{
MessageBox.Show(this, "동영상 재생에 실패하였습니다. 파일을 확인해주십시오.", "파일 열기");
return;
}
// 동영상 객체에 포함된 Channel 객체 중 해당 Channel 객체를 활성화
// 활성화된 객체만 스트리밍이 수행
VS.videoChannel.Activate();
}
catch (Exception ex)
{
Console.WriteLine(ex);
MessageBox.Show(this, "재생 실패!", "동영상");
VS.action = VideoAction.STOP;
}
}
C#
private void OnPlay()
{
if (VS.action == VideoAction.PAUSED) // 동영상이 중지 상태가 아닐경우
{
VS.videoChannel.Resume(); // 동영상 채널 Resume
VS.action = VideoAction.PLAYING;
}
else if (VS.action == VideoAction.STOP) // 동영상이 중지 상태일 경우
{
// 동영상 Channel을 처음부터 재생하다록 Play 신호 설정
VS.videoChannel.Play();
VS.action = VideoAction.PLAYING;
}
}
// 동영상 멈춤버튼클릭 이벤트
private void OnPause()
{
if (VS.action == VideoAction.PLAYING)
{
VS.videoChannel.Pause(); // 동영상 채널 Pause
VS.action = VideoAction.PAUSED;
}
}
// 동영상 정지버튼클릭 이벤트
public void OnStop()
{
if (VS.videoChannel != null)
{
// 재생 Frame Buffer를 삭제
VS.videoChannel.ClearFrameBuffer();
// 동영상 재생 스크린을 갱신한다.
nxVideoView1.RefreshScreen();
// 동영상 재생 중지
VS.videoChannel.Stop();
}
// 동영상 채널 정보 초기화
nxVideoView1.ResetVideoChannel();
if (VS.video != null)
{
// 동영상 객체 Close
VS.video.Close();
VS.video = null;
}
VS.action = VideoAction.STOP;
}
C#
private void button_Play_Click(object sender, EventArgs e)
{
// 비디오가 Stop인 상태인 경우 다시 비디오를 저장된 파일 경로로부터 Open해서 설정한다.
if (VS.action == VideoAction.STOP)
{
OnOpen();
}
// 설정된 비디오를 Play한다.
OnPlay();
}
private void button_Pause_Click(object sender, EventArgs e)
{
// Play되고 있는 비디오를 Pause한다.
OnPause();
}
private void button_Stop_Click(object sender, EventArgs e)
{
// Play되고 있는 비디오를 Stop한다.
OnStop();
}
프로젝트 이름은 “XDL_VideoView2”로 한다.
XAML
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="30"/>
<RowDefinition Height="*"/>
<RowDefinition Height="40"/>
</Grid.RowDefinitions>
<Menu Grid.Row="0">
<MenuItem Header="FIle" Margin="5,5,4,5">
<MenuItem x:Name="openFileMenuItem" Header="_Open" Click="openFileMenuItem_Click"/>
</MenuItem>
</Menu>
</Grid>
아래의 표를 참고하여 메뉴를 생성한다.
Control Type | Header | Name |
---|---|---|
MenuItem | _File | |
MenuItem | _Open | openFileMenuItem |
샘플코드에서 이미지를 다운 받아 [속성] 창에 “브러시” 속성을 통해 알맞은 공간에 배치한다. 작업을 완료하면 아래와 같은 디자인을 얻을 수 있다. (자세한 설명은 샘플코드를 참고한다.)
XAML
<Grid Grid.Row="1">
<WindowsFormsHost Grid.Column="0" Margin="3,0,3,3">
<nxVideo:NXVideoView x:Name="nxVideoView1">
<nxVideo:NXVideoView.Controls>
<nxVideo:NXVideoLayerOverlay x:Name="nxVideoLayerOverlay1"/>
</nxVideo:NXVideoView.Controls>
</nxVideo:NXVideoView>
</WindowsFormsHost>
</Grid>
C#
private void openFileMenuItem_Click(object sender, RoutedEventArgs e)
{
}
C#
using Pixoneer.NXDL;
using Pixoneer.NXDL.NXVideo;
C#
public partial class MainWindow : Window
{
enum VideoAction { STOP, PLAYING, PAUSED }
struct VideoState
{
public XVideo video; // 파일이나 네트워크로부터 입력되는 스트리밍데이터를 제어하는 기능을 수행할 객체 선언
public XVideoChannel videoChannel; // 동영상 개체에 포함된 채널 객체 선언
public string videoFilePath; // 동영상 파일 경로
public VideoAction action; // 비디오 플레이 상태를 정의하는 객체 선언
}
private XVideoIO m_videoIO = null;
private VideoState VS; // 비디오 상태를 관리하는 객체 선언
C#
public MainWindow()
{
InitializeComponent();
m_videoIO = new XVideoIO(); // VideoIO를 생성
VideoInit();
nxVideoLayerOverlay1.LayerVisible = true;
}
private void VideoInit()
{
// VideoState의 초기화
VS.video = null;
VS.videoChannel = null;
VS.videoFilePath = string.Empty;
VS.action = VideoAction.STOP;
}
private void Window_Closing(object sender, CancelEventArgs e)
{
// 동영상 채널 정보 초기화
nxVideoView1.ResetVideoChannel();
if (VS.video != null)
{
// 동영상 객체 Close
VS.video.Close();
VS.video = null;
}
if (m_videoIO != null)
{
// 동영상 입출력 객체 Dispose
m_videoIO.Dispose();
}
}
C#
private void openFileMenuItem_Click(object sender, RoutedEventArgs e)
{
// 새로운 파일 Open을 수행한다.
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "TS file(*ts)|*.ts||";
openFileDialog.RestoreDirectory = true;
Nullable<bool> result = openFileDialog.ShowDialog();
if (result != true) return;
string videoPath = openFileDialog.FileName;
// 동영상 파일의 존재 유무 체크
if (System.IO.File.Exists(videoPath) == false)
{
MessageBox.Show(this, "해당 경로에 영상이 없습니다.", "오류");
return;
}
// 파일 경로를 저장한다.
VS.videoFilePath = videoPath;
// 동영상의 재생상태가 중지가 아닐 경우 동영상 재생 중지
OnStop();
// 동영상 스트리밍 환경을 생성한다.
OnOpen();
// 동영상을 Play한다.
OnPlay();
}
C#
// 동영상 파일 열기
public void OnOpen()
{
try {
string strError = null;
// 동영상 스트림 정보 가져오기
VS.video = m_videoIO.OpenFile(VS.videoFilePath, "XFFMPDRIVER", out strError);
if (VS.video == null)
{
MessageBox.Show(this, "동영상 재생에 실패하였습니다. 파일을 확인해주세요.", "파일 열기");
return;
}
// 다중 채널을 가진 동영상 객체의 Channel 인덱스
int nIdxChannel = 0;
// 동영상 뷰에 재생할 동영상 채널의 설정
nxVideoView1.SetVideoChannel(VS.video, nIdxChannel);
// 입력 인덱스에 해당하는 Channel 가져오기
VS.videoChannel = VS.video.GetChannel(nIdxChannel);
// GetChannel에 실패할 경우 Null 객체가 return되며, 그에 대한 예외처리
if (VS.videoChannel == null)
{
MessageBox.Show(this, "동영상 재생에 실패하였습니다. 파일을 확인해주세요.", "파일 열기");
return;
}
// 동영상 객체에 포함된 Channel 객체 중 해당 Channel 객체를 활성화
// 활성화된 객체만 스트리밍이 수행
VS.videoChannel.Activate();
} catch (Exception e)
{
Console.WriteLine(e);
MessageBox.Show(this, "재생 실패!", "동영상");
VS.action = VideoAction.STOP;
}
}
C#
private void OnPlay()
{
if (VS.action == VideoAction.PAUSED) // 동영상이 중지 상태가 아닐경우
{
VS.videoChannel.Resume(); // 동영상 채널 Resume
VS.action = VideoAction.PLAYING;
} else if (VS.action == VideoAction.STOP) // 동영상이 중지 상태일 경우
{
// 동영상 Channel을 처음부터 재생하도록 Play 신호 설정
VS.videoChannel.Play();
VS.action = VideoAction.PLAYING;
}
}
private void OnPause()
{
if (VS.action == VideoAction.PLAYING)
{
VS.videoChannel.Pause(); // 동영상 채널 Pause
VS.action = VideoAction.PAUSED;
}
}
private void OnStop()
{
if (VS.videoChannel != null)
{
// 재생 Frame Buffer를 삭제
VS.videoChannel.ClearFrameBuffer();
// 동영상 재생 스크린을 갱신한다.
nxVideoView1.RefreshScreen();
// 동영상 재생 중지
VS.videoChannel.Stop();
}
// 동영상 채널 정보 초기화
nxVideoView1.ResetVideoChannel();
if (VS.video != null)
{
// 동영상 객체 Close
VS.video.Close();
VS.video = null;
}
VS.action = VideoAction.STOP;
}
C#
private void playButton_Click(object sender, RoutedEventArgs e)
{
// 비디오가 Stop 상태인 경우 다시 비디오를 저장된 파일 경로로부터 Open해서 설정한다.
if (VS.action == VideoAction.STOP)
{
OnOpen();
}
// 설정된 비디오를 Play한다.
OnPlay();
}
private void pauseButton_Click(object sender, RoutedEventArgs e)
{
// Play되고 있는 비디오를 Pause한다.
OnPause();
}
private void stopButton_Click(object sender, RoutedEventArgs e)
{
// Play되고 있는 비디오를 Stop한다.
OnStop();
}